/*
 * @Author: Klein
 * @Date: 2021-06-17 11:07:28
 * @Description: file content
 */
const router = require('koa-router')();
const { WEATHER_PARAMS } = require('../config/index');
const Message = require('../lib/message');
const { proxyGet } = require('../lib/axios');
const path = require('path');
// const xlsx = require('node-xlsx');
const fs = require('fs');
router.get('/', async (ctx, next) => {
	const json = fs.readFileSync(path.join(__dirname, '../', '15.json'));
	const json1 = fs.readFileSync(path.join(__dirname, '../', '石膏板.json'));
	const json2 = fs.readFileSync(path.join(__dirname, '../', '腻子粉.json'));
	const json3 = fs.readFileSync(path.join(__dirname, '../', '电线.json'));
	const json4 = fs.readFileSync(path.join(__dirname, '../', '轻钢龙骨.json'));
	const json5 = fs.readFileSync(path.join(__dirname, '../', '弱电线.json'));
	const json6 = fs.readFileSync(path.join(__dirname, '../', '阻燃-钉螺丝.json'));

	const data = [
		...JSON.parse(json),
		...JSON.parse(json1),
		...JSON.parse(json2),
		...JSON.parse(json3),
		...JSON.parse(json4),
		...JSON.parse(json5),
		...JSON.parse(json6),
	];
	const brand = fs.readFileSync(path.join(__dirname, '../', '品牌.json'));

	const _newData = data
		.filter((item) => item.name)
		.map((item) => {
			const b = JSON.parse(brand).find((i) => i.name === item.brandId);
			return {
				...item,
				brandId: b ? b.id : item.brandId,
			};
		});

	ctx.body = {
		length: [
			...JSON.parse(json),
			...JSON.parse(json1),
			...JSON.parse(json2),
			...JSON.parse(json3),
			...JSON.parse(json4),
			...JSON.parse(json5),
			...JSON.parse(json6),
		].length,
		data: _newData,
	};
});

router.get('/common/weather', async (ctx) => {
	const result = await proxyGet('https://tianqiapi.com/api', WEATHER_PARAMS);
	ctx.body = Message.success(result);
});

module.exports = router;
